//
//  UserInfoViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/23.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "UserInfoViewController.h"

@interface UserInfoViewController ()
<
UITextFieldDelegate,
YYTextViewDelegate
>
{
    UIScrollView * scrollView;
    UIImageView * headerImageView;
    UIImage * headerImage;
    UITextField * nickTextField;
    UITextField * areaTextField;
    UITextField * addressTextField;
    UITextField * phoneTextField;
    UITextField * passTextField;
    YYTextView * textView;
    CGPoint point;
    
    BOOL isTextView;
}

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑资料";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self setBackRightBarButtonItemAction:@selector(saveBtnAction:) text:@"保存"];
    [self initView];
    isTextView = NO;
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)saveBtnAction:(UIButton *)sender{
    
    WS(weakSelf);
    
    NSString * intro = textView.text;
    NSString * nickname = nickTextField.text;
    NSString * addr = addressTextField.text;
    NSString * area = areaTextField.text;
    NSString * phone = phoneTextField.text;
    
    
    [self showHudInView:self.view hint:@""];
    [HttpRequestHelper  requestApiUserEditIntro:intro nickname:nickname addr:addr area:area avatar:headerImage mobile:phone Block:^(NSString *msg, UserModel *model) {
        [weakSelf hideHud];
        if (REQUEST_SUCCESS) {
            
            [weakSelf backAction:nil];
            [weakSelf showHint:@"修改成功"];
            
        }else{
            
            [weakSelf showHint:msg];
        }
    }];  
}

- (void)initView{
    
    scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *vContainer = [UIView new];
    [scrollView addSubview:vContainer];
    [vContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    UIView * topView = [[UIView alloc]init];
    [topView setBackgroundColor:HexRGBA(0xfbf5ec, 1)];
    [vContainer addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(vContainer);
        make.height.mas_equalTo(56 * kAutoScanleX);
    }];
    
    
    UILabel * labelTitle = [[UILabel alloc]init];
    [labelTitle setNumberOfLines:0];
    [labelTitle setText:@"请完善个人资料，您所填写的各项资料都会妥善处理，\n我们绝对不会泄露您的个人信息"];
    [labelTitle setTextColor:HexRGBA(0xbc9a6d, 1)];
    [labelTitle setFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
    [labelTitle setTextAlignment:NSTextAlignmentCenter];
    [topView addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(topView);
    }];
    
    
    NSArray * array = @[@"头        像",@"昵        称",@"所在地区",@"详细地址",@"手机号码",@"简        介"];
    NSArray * pArray = @[@"",@"请填写昵称",@"请选择所在地区",@"请输入详细地址",@"请输入手机号码",@""];
    UIView * lastView = nil;
    NSInteger count = array.count;
    
    for (NSInteger i = 0; i < count; i++) {
        
        UIView * item = [[UIView alloc]init];
        [item setBackgroundColor:[UIColor whiteColor]];
        [vContainer addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lastView == nil ? topView.mas_bottom :lastView.mas_bottom);
            make.left.right.equalTo(vContainer);
            make.height.mas_equalTo(55 * kAutoScanleX);
        }];
        
        UILabel *label = [[UILabel alloc]init];
        [label setTextColor:HexRGBA(0x585858, 1)];
        [label setText:array[i]];
        [label setFont:[UIFont systemFontOfSize:14 * kAutoScanleX]];
        [label setTextAlignment:NSTextAlignmentRight];
        [item addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == count - 1) {
                make.top.equalTo(item).offset(18 * kAutoScanleX);
            }else{
                 make.centerY.equalTo(item);
            }
            make.left.equalTo(item).offset(5 * kAutoScanleX);
            make.width.mas_equalTo(72 * kAutoScanleX);
            make.height.mas_equalTo(25 * kAutoScanleX);
        }];
        
        
        [self setBomLine:item color:HexRGBA(0xd5d9db, 1)];
        
        if (i == 0) {
            
            [item mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(72 * kAutoScanleX);
            }];
        
            headerImageView = [[UIImageView alloc]init];
            [item addSubview:headerImageView];
            [headerImageView setImage:[UIImage imageNamed:@"icon_bianji_ziliao_touxiang"]];
            [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.right.equalTo(item).offset(-15 * kAutoScanleX);
                make.size.mas_equalTo(CGSizeMake(50 * kAutoScanleX, 50 * kAutoScanleX));
            }];
            
            UIButton * button = [[UIButton alloc]init];
            [button addTarget:self action:@selector(buttonHeaderAction:) forControlEvents:UIControlEventTouchUpInside];
            [item addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(item);
            }];
            
            
        }
        
        
        if (i != 0 && i != count -1) {
            
            UITextField * textField =[[UITextField alloc]init];
            [textField setDelegate:self];
            [item addSubview:textField];
            [textField setTextColor:HexRGBA(0x000000, 1)];
            [textField setKeyboardType:UIKeyboardTypeEmailAddress];
            [textField setReturnKeyType:UIReturnKeyDone];
            [textField setFont:[UIFont systemFontOfSize:14]];
            [textField setValue:HexRGBA(0xa7a7a7, 1) forKeyPath:@"_placeholderLabel.textColor"];
            [textField setPlaceholder:pArray[i]];
            [textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(item);
                make.left.equalTo(label.mas_right).offset(15 * kAutoScanleX);
                make.right.equalTo(item).offset(-15 * kAutoScanleX);
                make.height.mas_equalTo(30 * kAutoScanleX);
            }];
            
            if (i == 1) {
                
                nickTextField = textField;
            }
            
            if (i == 2) {
                
                areaTextField = textField;
                
                [textField mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(item).offset(-45 * kAutoScanleX);
                }];
            
                [self setBackRightArrow:item];
                
                UIButton * button = [[UIButton alloc]init];
                [button addTarget:self action:@selector(buttonAddressAction:) forControlEvents:UIControlEventTouchUpInside];
                [item addSubview:button];
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(item);
                }];
                
            }
            
            if (i == 3) {
                
                addressTextField = textField;
            }
            
            if (i == 4) {
                
                phoneTextField = textField;
            }
            
        }
        
        if (i == count -1) {
            
            [item mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lastView.mas_bottom).offset(10 * kAutoScanleX);
                make.height.mas_equalTo(140 * kAutoScanleX);
            }];
            
            
            textView = [[YYTextView alloc]init];
            [textView setBackgroundColor:[UIColor whiteColor]];
            [item addSubview:textView];
            [textView setDelegate:self];
            [textView setPlaceholderText:@"用一两句话介绍自己吧~"];
            [textView setReturnKeyType:UIReturnKeyDone];
            [textView setFont:[UIFont systemFontOfSize:15 *kAutoScanleX]];
            [textView setPlaceholderFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
            [textView setTextColor:HexRGBA(0x000000, 1)];
            [textView setPlaceholderTextColor:HexRGBA(0xb3b5b8, 1)];
            [textView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.equalTo(item).insets(UIEdgeInsetsMake(15 * kAutoScanleX, 15 * kAutoScanleX, 15 * kAutoScanleX, 15 * kAutoScanleX));
                make.left.equalTo(label.mas_right).offset(15 * kAutoScanleX);
            }];
    
        }
     
        lastView = item;
    }
    
    
    UserModel * model = [UserCenterHelper shareInstance].userModel;
    
    if (model) {
    
        [headerImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"icon_bianji_ziliao_touxiang"]];
        [nickTextField setText:model.nickname];
        [areaTextField setText:model.area];
        [addressTextField setText:model.addr];
        [phoneTextField setText:model.mobile];
        [textView setText:model.intro];
    }
    
    
    
    [vContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView).offset(30 * kAutoScanleX);
    }];
    
}

#pragma mark -- 头像
- (void)buttonHeaderAction:(UIButton *)sender{
    
    WS(weakSelf);
    
    [self upLoadPhoto];
    [self setFinishPhoto:^(UIImage *image) {
        [weakSelf headerFinishImage:image];
    }];
}


- (void)headerFinishImage:(UIImage *)image{
    
    [headerImageView setImage:image];
    headerImage = image;
    
}

#pragma mark -- 选择地区
- (void)buttonAddressAction:(UIButton *)sender{
    
    CityListViewController * vc = [[CityListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc setBlcokFinish:^(CityIndexModel *model, NSString *address) {
        [areaTextField setText:address];
    }];
    
}

#pragma mark -- 代理
- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        
        [ApplicationDelegate.window endEditing:YES];
        
        return NO;
    }
    
    
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(YYTextView *)textView{
    
    isTextView = YES;
    
    point = [textView convertPoint:textView.bounds.origin toView:scrollView];
    
    NSLog(@"----------------%f-----------------",point.y);
    
    return YES;
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
   isTextView = NO;
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
}


#pragma mark--弹起键盘
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

#pragma mark--弹起键盘
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardF.size.height;
    
    
    [UIView animateWithDuration:0.25 animations:^{
        
        if (isTextView) {
           
            [scrollView setContentOffset:CGPointMake(0,point.y - 100 * kAutoScanleX) animated:YES];
        }
     
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).offset(-height);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
    
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view);
        }];
        
        [self.view layoutIfNeeded];
        
    }];
    
}

@end
